package com.jinlia.show.common.web.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jinlia.show.common.core.utils.UserInfoUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyBaitsBaseEntityHandler implements MetaObjectHandler {
    private final Logger logger = LoggerFactory.getLogger(MyBaitsBaseEntityHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        logger.info("start insert fill ....");
        this.strictInsertFill(metaObject, "creator", Long.class, UserInfoUtils.getUserId()); // 起始版本 3.3.0(推荐使用)
         // 起始版本 3.3.0(推荐)
        this.strictInsertFill(metaObject, "lastUpdater", Long.class, UserInfoUtils.getUserId()); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "lastUpdateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        logger.info("start update fill ....");
        this.strictUpdateFill(metaObject, "lastUpdater", Long.class, UserInfoUtils.getUserId()); // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "lastUpdateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.3(推荐)
    }
}
